package org.wanda.pv.controller.business;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.basic.Specialty;
import org.wanda.pv.model.basic.System;
import org.wanda.pv.model.business.Drawing;
import org.wanda.pv.model.file.FileUploadInfo;
import org.wanda.pv.model.hy.HyDrawing;
import org.wanda.pv.service.hy.HyDrawingService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxy
 * @date 2018/11/23 10:27
 */

@SuppressWarnings("all")
public class DrawingController {
    @Autowired
    Dao dao;


    private final static Logger log = LoggerFactory.getLogger(DrawingController.class);



    @RequestMapping("/getDrawing")
    public Object getDrawing(String projectFlowChildrenNodeId){
        List<Drawing> wdDrawings=dao.query(Drawing.class, Cnd.where("project_flow_children_node_id","=",projectFlowChildrenNodeId).and("delFlag","=","0"));
        wdDrawings.forEach(item->{
            dao.fetchLinks(item,null);
        });
        return Result.success("success",wdDrawings);

    }

    /**
     *  查询系统下的所有图纸
     * @param systemId
     * @param projectFlowChildrenNodeId
     * @return
     */
    @RequestMapping("/getDrawingBySysId")
    public Object getDrawingBySystemId(Long systemId,String projectFlowChildrenNodeId){
        try {
            List<Drawing> wdDrawings=dao.query(Drawing.class, Cnd.where("system_id","=",systemId).and("project_flow_children_node_id","=",projectFlowChildrenNodeId).and("delFlag","=","0"));
            wdDrawings.forEach(item->{
                dao.fetchLinks(item,null);
            });
            return Result.success("success",wdDrawings);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error("fail","服务器内部错误");
        }
    }



    /**
     * 根据图纸id获取所属专业和系统
     * @param id  图纸id
     * @return
     */
    @RequestMapping(value = "/getSpecAndSystem", method = RequestMethod.POST)
    public Object getSpecialtyAndSystem(Long id){
        try {
            Map<String,Object> map=new HashMap<>();
            Drawing wdDrawing= dao.fetch(Drawing.class,Cnd.where("id", "=", id).and("delFlag", "=", false));
            System system=dao.fetch(System.class,wdDrawing.getSystemId());
            Specialty specialty=dao.fetch(Specialty.class,system.getSpecialtyId());
            map.put("system",system);
            map.put("specialty",specialty);
            return Result.success("success", map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }
    }
}
